//
//  SlideRound.m
//  BankOfShangHai
//
//  Created by dxl on 11-11-10.
//  Copyright 2011 P&C. All rights reserved.
//

#import "PCShopSlideRound.h"


#define Define_RoundSize 12.0f

#define Define_RoundSpace 10.0f

@interface PCShopSlideRound()

@property (nonatomic,strong) NSArray *imageArray;

@end

@implementation PCShopSlideRound

@synthesize imageArray;

@synthesize sizeOfRound =_sizeOfRound;

@synthesize spaceWithRound =_spaceWithRound;

@synthesize count =_count;

@synthesize imageOfSelected;
@synthesize imageOfUnSelected;
@synthesize alignment =_alignment;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        
        self.backgroundColor =[UIColor clearColor];
        
        _slideFrame =frame;
        
		_slideSize =frame.size;
        
        _sizeOfRound =CGSizeMake(Define_RoundSize, Define_RoundSize);
        
        _spaceWithRound =Define_RoundSpace;
        
        _selectIndex =0;
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame andRoundCount:(int)count{

	if (self =[self initWithFrame:frame]) {
		
        [self setCount:count];
	}
	return self;
}

-(void)layoutSubviews{
	
    CGFloat width =_slideFrame.size.width;

    CGFloat firstRoundOrigin_X =0;

//    firstRoundOrigin_X = width_X - self.count *(_sizeOfRound.width+_spaceWithRound);
    
    switch (_alignment) {
        case SlideRoundAlignmentLeft:
        {
            firstRoundOrigin_X =0;
        }
            break;
            
        case SlideRoundAlignmentRight:
        {
            firstRoundOrigin_X =width -self.count *(_sizeOfRound.width+_spaceWithRound);
        }
            break;
            
        case SlideRoundAlignmentCenter:
        {
            if ((self.count %2) ==0) {
                
                firstRoundOrigin_X =width/2 -self.count *(_sizeOfRound.width+_spaceWithRound)/2 +_spaceWithRound/2;
                
            }
            else{
                firstRoundOrigin_X =width/2 -self.count *(_sizeOfRound.width+_spaceWithRound)/2 -_sizeOfRound.width/2;
            }
        }
            break;
            
        default:
        {
            firstRoundOrigin_X =0;
        }
            break;
    }
    
	for (int i=0; i<self.count; i++) {
		
		UIImageView *imgView =[self.imageArray objectAtIndex:i];
		
		if (i ==0) {
			imgView.image =self.imageOfSelected;
		}
        else{
            imgView.image =self.imageOfUnSelected;
        }

        CGRect tempRect =CGRectMake(firstRoundOrigin_X +i*(_sizeOfRound.width +_spaceWithRound), 0, _sizeOfRound.width, _sizeOfRound.height);
        
		imgView.frame =tempRect;
		
		imgView.alpha =0.8;
        
		[self addSubview:imgView];
	}
}

-(void)SlideRoundSelectIndex:(int)index{
    
	for (int i=0; i<self.count; i++) {
		
		UIImageView *imgView =[self.imageArray objectAtIndex:i];
		
		if (i ==index) {
			imgView.image =self.imageOfSelected;
		}
		else {
			imgView.image =self.imageOfUnSelected;
		}
	}
}

-(void)setSizeOfRound:(CGSize)sizeOfRound{

    _sizeOfRound =sizeOfRound;
    
    [self layoutSubviews];
}

-(void)setSpaceWithRound:(float)spaceWithRound{

    _spaceWithRound =spaceWithRound;
    
    [self setNeedsLayout];
}

-(void)setCount:(NSUInteger)count{

    for (id obj in self.imageArray) {
        if ([obj isKindOfClass:[UIView class]]) {
            UIView *v =(UIView *)obj;
            [v removeFromSuperview];
        }
    }
    
    _count =count;
    
    NSMutableArray *array =[[NSMutableArray alloc] initWithCapacity:_count];
    
    for (int i=0; i<_count; i++) {
        
        UIImageView *imgView =[[UIImageView alloc] init];
        imgView.image =self.imageOfUnSelected;
        
        [array addObject:imgView];
    }
    
    self.imageArray =[NSArray arrayWithArray:array];
    
    [self setNeedsLayout];
}


-(void)setAlignment:(SlideRoundAlignment)alignment{

    _alignment =alignment;
    
    [self setNeedsLayout];
}

@end
